/*
 * Copyright 2007 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


package bw.weather;

import javax.servlet.*;
import java.util.*;
import bw.util.*;

public class WeatherServant implements Servant
{
    public static final String DO_REFRESH = "doWeatherRefresh";
    
    public void init ( ServletContext context )
    {
        //set up the config params
        String licenseKey = context.getInitParameter( "weatherKey" );
        String partnerId = context.getInitParameter( "weatherPartnerId" );
        String locationId = context.getInitParameter( "weatherLocationId" );
        String forecastLengthStr = context.getInitParameter( "weatherForecastLength" );
        String updateIntervalStr = context.getInitParameter( "weatherUpdateInterval" );

        int forecastLength = 5;
        try
        {
            forecastLength = Integer.parseInt( forecastLengthStr );
        }
        catch ( NumberFormatException ex )
        {
            Log.getInstance().write( "WeatherServant: Bad format for forecastlength: " + 
                                     forecastLengthStr + " (Using default of 5 days)" );
        }

        int updateInterval = 120;
        try
        {
            updateInterval = Integer.parseInt( updateIntervalStr );
        }
        catch ( NumberFormatException ex )
        {
            Log.getInstance().write( "WeatherServant: Bad format for updateInterval: " +
                                     updateIntervalStr + " (Using default of 60 minutes)" );
        }

        WeatherLocationFactory factory = WeatherLocationFactory.getInstance();
        factory.init( partnerId, licenseKey, forecastLength, updateInterval );

        WeatherManager manager = WeatherManager.getInstance();

        //multiple locations are separated by a comma in the web.xml file
        //StringTokenizer st = new StringTokenizer( locationId, "," );
        //while ( st.hasMoreTokens() )
        //{
        //    WeatherLocation loc = factory.createWeatherLocation( st.nextToken().trim() );
        //    manager.addLocation( loc );
        //}

        //add an arbitrary location
        WeatherLocation other = new WeatherLocation( partnerId, licenseKey, 
                                                     forecastLength,
                                                     updateInterval );
        manager.addOtherLocation( other );

        String shouldRefresh = context.getInitParameter( "doBackgroundWeatherRefresh" );
        if ( "true".equalsIgnoreCase( shouldRefresh ) ) 
        {
            Config.setProperty( WeatherServant.DO_REFRESH, true );
            
            RefresherPool.getInstance().addSource( new WeatherRefresherSource() );
        }
        else
        {
            Config.setProperty( WeatherServant.DO_REFRESH, false );
            Log.getInstance().write( "WeatherServant: WeatherRefresher is disabled." );
        }

    }

    public void shutdown ( ServletContext context )
    {
        //nothing
    }
}
